;layui.define(["admin","table","form","treeGrid"], function(exports) {
    var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,treeGrid = layui.treeGrid
        ,tableId = "menu_list_table"
        ,searchFormId = "#res-list-search-form"
        ,admin = layui.admin;
    treeGrid.render({
        id:tableId,
        elem: "#"+tableId,
        url: '/sso/admin/menu/list',
        method:'post',
        parseData:function (res) {
            return{
                "code":   res.status?res.status:0, //解析接口状态
                "msg": '', //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.records //解析数据列表
            }
        },
        idField:'id',
        treeId:'id',
        treeUpId:'parentId',
        treeShowName:'name',
        isOpenDefault:true,
        iconOpen:false,
        isPage:false,
        heightRemove:[".dHead",200],
        height:'80%',
        cols: [[{
            type: "checkbox",
            fixed: "left",
            width: "5%",
        },{
            field: "name",
            title: "账户名称",
            width: "20%",
        }, {
            field: "resUrl",
            title: "权限标识",
            width: "15%",
        }, {
            field: "menuUrl",
            title: "菜单URL",
            width: "15%",
        }, {
            field: "type",
            title: "资源类型",
            width: "10%",
            templet: "#typeTpl",
            align:'center'
        },{
            field: "status",
            title: "资源状态",
            templet: '#statusTpl',
            width: "15%",
            align:'center'
        }, {
            title: "操作",
            align: "center",
            width: "20%",
            fixed: "right",
            toolbar: "#resource-list-toolbar"
        }]],
        page: true,
        limit: 30,
        text: {
            none: '暂无相关数据' //默认：无数据
        },
        done:function (res, curr, count) {
            this.where ={};
            if(res.code == 401){
                layer.msg("没有操作权限,请联系管理员");
            }
            if(res.code == 1001){
                layer.open({
                    title: '提示信息'
                    ,content: '登录失效,请重新登录！',
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    end: function(index, layero){
                        window.location.href="/sso/logout";
                    },
                    time:5000
                });
            }
        }
    });
    treeGrid.on("tool("+tableId+")", function (e) {
        var row = e.data;
        if ("del" === e.event) layer.prompt({
            formType: 0,
            title: "敏感操作，输入口令"
        }, function (value,index) {
            if(value == 'ok'){
                layer.close(index);
                layer.confirm('确定删除行吗？', function (t) {
                    admin.req({
                        url:'/sso/admin/menu/remove',
                        data:{ids:row.id},
                        done:function (data) {
                            if(data.status == 200){
                                e.del(), layer.close(t),layer.msg("删除成功"),table.reload(tableId);
                            }else{
                                layer.close(t),layer.msg("删除失败，请重试");
                            }
                        }
                    });
                });
            }else{
                layer.msg('口令错误');
            }
        });
        if ("edit" === e.event) {
            $(e.tr);
            layer.open({
                type: 2,
                title: "编辑资源 EDIT",
                content: '/sso/admin/menu/toEditPage?id='+row.id,
                maxmin: !0,
                area: ["500px", "600px"]
            })
        }
        if("add" === e.event){
            layer.open({
                type: 2,
                title: "新增资源 ADD",
                content: '/sso/admin/menu/toSavePage?id='+row.id,
                maxmin: !0,
                area: ["500px", "600px"]
            })
        }
    });

    //监听锁定操作
    form.on('switch(statusSwitch)', function(obj){
        layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
    });

    //事件
    var active = {
        batchDel: function () {
            var checkStatus = table.checkStatus('resources_list_table')
                , checkData = checkStatus.data
            var deList = []; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            checkData.forEach(function (row,index) {
                deList.push(row.id);
            });

            layer.prompt({
                formType: 0
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                if(value == 'ok'){
                    layer.close(index);
                    layer.confirm('确定删除吗？', function (index) {
                        $.get("/sso/admin/menu/remove",{ids:deList.join()},function(data){
                            if(data.status == 200){
                                table.reload('menu_list_table');
                                layer.msg("删除成功");
                            }else{
                                layer.msg("删除失败，请重试");
                            }
                        });

                    });
                }else{
                    layer.msg('口令错误');
                }

            });
        },open:function () {
            var treeData=treeGrid.getDataTreeList(tableId);
            var that= !treeData[0][treeGrid.config.cols.isOpen];
            treeGrid.treeOpenAll(tableId,that);
        },add: function () {
            var url = "menu/add";
            layer.open({
                type: 2,
                title: '添加用户 ADD',
                content: '/sso/admin/forward/systemManage?url='+url,
                maxmin: true,
                area: ['500px', '600px']
            });
        },reload: function () {
            var field = $(searchFormId).serializeObject();
            treeGrid.reload(tableId,{
                where:field
            });

        },reset: function () {
            treeGrid.reload(tableId);
        }
    };

    $('.toolbar-button').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports("menu", {});
});
